<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/16
 * Time: 11:21
 */

namespace Multiple\Home\Controllers;
use Asa\Erp\TbMember;
use Phalcon\Paginator\Adapter\Model;
use Exception;


class MemberController extends CadminController{

    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize();
        $this->setModelName('Asa\\Erp\\TbMember');
    }


    //会员管理列表
    public function pageAction()
    {
        // 如果未登录，那么默认为 cn
        if (!$lang = $this->getDI()->get("session")->get("language")) {
            $lang = 'cn';
        }

        $params = $this->request->getPost();
        $where = [
            sprintf("companyid=%d", $this->companyid),
        ];
        if (isset($params['phoneno1']) && !empty($params['phoneno1'])) {
            $where[] = sprintf("phoneno like '%%%s%%'", addslashes(strtoupper($params["phoneno1"])));
        }
        $filter = implode(' and ', $where);
        $result = TbMember::find($filter);

        $page = $this->request->getPost("page", "int", 1);
        $pageSize = $this->request->getPost("pageSize", "int", 20);

        $paginator = new Model(
            [
                "data"  => $result,
                "limit" => $pageSize,
                "page"  => $page,
            ]
        );   // Get the paginated results
        $pageObject = $paginator->getPaginate();

        $data = [];
        foreach ($pageObject->items as $row) {
            $rowData = $row->toArray();
            $rowData['name'] = $row->getName();
            $rowData['contactor_name'] = $row->getUserLoginName($row->creatorid);
            $data[] = $rowData;
        }

        $pageinfo = [
            "current"    => $pageObject->current,
            "totalPages" => $pageObject->total_pages,
            "total"      => $pageObject->total_items,
            "pageSize"   => $pageSize,
        ];

        // 返回
        echo $this->reportJson(["data" => $data, "pagination" => $pageinfo], 200, []);
    }

    //用于销售查询会员接口
    public function getListAction(){
        // 如果未登录，那么默认为 cn
        if (!$lang = $this->getDI()->get("session")->get("language")) {
            $lang = 'cn';
        }
        $where = [
            sprintf("companyid=%d", $this->companyid),
            "order" => "id DESC",
        ];
        $result = TbMember::find($where);
        echo  $this->success($result);
    }

    //添加
    public function addAction()
    {
        if ($this->request->isPost()) {
            $name           = $_POST['name'];
            $phoneno        = $_POST['phoneno'];
            $birthday       = $_POST['birthday'];
            $email          = $_POST['email'];
            $qq             = $_POST['qq'];
            $wechat         = $_POST['wechat'];
            $creatorid      = $_POST['creatorid'];
            $idno           = $_POST['idno'];
            $code           = $_POST['code'];
            $membercard     = $_POST['membercard'];
            $created_at     = date('Y-m-d H:i:s');

            $model = new TbMember();
            $model->name        = $name;
            $model->phoneno     = $phoneno;
            $model->birthday    = $birthday;
            $model->email       = $email;
            $model->qq          = $qq;
            $model->wechat      = $wechat;
            $model->creatorid   = $creatorid;
            $model->idno        = $idno;
            $model->code        = $code;
            $model->membercard  = $membercard;
            $model->created_at  = $created_at;
            $model->companyid   = $this->companyid;


            $result = ["code" => 200, "messages" => []];
            if ($model->create() === false) {
                $this->logFile->log("添加失败，原因是：".$this->error($model));
                $message = json_decode($this->error($model),true);
                throw new Exception($message['messages'][0]);
            } else {
                $result['is_add'] = "1";
                $result['id'] = $model->id;
            }
            echo json_encode($result);
        }
    }

}